package com.example.weather.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.weather.R;
import com.example.weather.utils.DataHelper;

import java.util.HashMap;
import java.util.Map;

public class AddpPlanActivity extends AppCompatActivity {

    private TextView mDeleteTextView;
    private Button mSaveButton;
    private EditText mContentEditText;
    private Spinner mStartHourSpinner;
    private Spinner mStartMinuteSpinner;
    private Spinner mEndHourSpinner;
    private Spinner mEndMinuteSpinner;
    private ImageButton mBackImageButton;
    private DataHelper mDataHelper;
    private int mSelectedYear;
    private int mSelectedMonth;
    private int mSelectedDate;
    private boolean isExist;
    private TextView mTitleTextView;
    private Map<String,Object> mMapData;
    private String content;
    private String mStartTime;
    private String mEndTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addp_plan_activity);
        InitData();
        InitView();
    }

    private void InitData(){
        Intent intent = getIntent();
        mSelectedYear = intent.getIntExtra("year",0);
        mSelectedMonth = intent.getIntExtra("month",0);
        mSelectedDate = intent.getIntExtra("date",0);
        isExist = intent.getBooleanExtra("isExist",false);
        mMapData = new HashMap<>();
    }

    private void InitView(){
        ClickListener clickListener  =new ClickListener();
        mDeleteTextView = (TextView)findViewById(R.id.tv_delete);
        mSaveButton = (Button) findViewById(R.id.bt_submit);
        mContentEditText = (EditText)findViewById(R.id.et_content);
        mStartHourSpinner = (Spinner)findViewById(R.id.sp_start_hour);
        mStartMinuteSpinner = (Spinner)findViewById(R.id.sp_start_minute);
        mEndHourSpinner = (Spinner)findViewById(R.id.sp_end_hour);
        mEndMinuteSpinner = (Spinner)findViewById(R.id.sp_end_minute);
        mTitleTextView = (TextView)findViewById(R.id.tv_title);
        mBackImageButton = (ImageButton)findViewById(R.id.ib_back);
        mDataHelper = DataHelper.getInstance(getApplicationContext());

        mDeleteTextView.setOnClickListener(clickListener);
        mSaveButton.setOnClickListener(clickListener);
        mBackImageButton.setOnClickListener(clickListener);
        if(!isExist){
            mTitleTextView.setText("添加计划");
        }else{
            mMapData = mDataHelper.getPlan(mSelectedYear,mSelectedMonth,mSelectedDate);
            content = (String)mMapData.get("content");
            mStartTime = (String)mMapData.get("startTime");
            mEndTime = (String)mMapData.get("endTime");
            int startHour = Integer.parseInt(mStartTime.split(":")[0]);
            int startMinute = Integer.parseInt(mStartTime.split(":")[1]);
            int endHour = Integer.parseInt(mEndTime.split(":")[0]);
            int endMinute = Integer.parseInt(mEndTime.split(":")[1]);
            mStartHourSpinner.setSelection(startHour);
            mStartMinuteSpinner.setSelection(startMinute);
            mEndHourSpinner.setSelection(endHour);
            mEndMinuteSpinner.setSelection(endMinute);
            mContentEditText.setText(content);
        }
    }

    private class ClickListener implements View.OnClickListener {
        public void onClick(View v) {
            if (v.getId() == R.id.bt_submit) {
                String startHour = (String)mStartHourSpinner.getSelectedItem();
                String startMinute = (String)mStartMinuteSpinner.getSelectedItem();
                String endHour = (String)mEndHourSpinner.getSelectedItem();
                String endMinute = (String)mEndMinuteSpinner.getSelectedItem();
                String content = mContentEditText.getText().toString();
                mDataHelper.insertAndUpdatePlan(mSelectedYear,mSelectedMonth,mSelectedDate,content,startHour+":"+startMinute,endHour+":"+endMinute);
                finish();
            }else if (v.getId() == R.id.tv_delete) {

                AlertDialog.Builder builder = new AlertDialog.Builder(AddpPlanActivity.this);
                builder.setMessage("确定删除吗？");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDataHelper.deletePlan(mSelectedYear,mSelectedMonth,mSelectedDate);
                        dialog.dismiss();
                        finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }else if (v.getId() == R.id.ib_back) {
                finish();
            }
        }
    }
}
